'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  //登录
  async index() {
    const { ctx,app } = this;
    const {name,pass}=ctx.request.body
    const result=await app.mysql.get("users",
    {name:name,pass:pass})
    let userID=result.userID
    let username=result.name
    if(result){
      ctx.body = {
        code:200,
        token:{name,username,userID},
        userID
      }
    }
  }
  //注册
  async reg(){
    const { ctx,app } = this;
    const {name,pass}=ctx.request.body
    const result=await app.mysql.get("users",
    {name:name})
    if(result){
      ctx.body={
        code:0,
        mess:"用户已经存在"
      }
    }else{
      let data=await app.mysql.insert("users",
      {
        name:name,
        pass:pass,
        id:null,
        userID:0
      })
      ctx.body={
        code:1,
        mess:"注册成功"
      }
    }
  }
}

module.exports = HomeController;
